#include <stdio.h>
#include <stdlib.h>
#include "mathlink.h"

MLINK link;
MLEnvironment env;

void mathlink_init_(void)
{
    int error;
    int argc = 4;
    char * argv[5] = {
            "-linkname",
            "math -mathlink",
            "-linkmode",
            "create",
            NULL
        };

    /* setup connection */
    env = MLInitialize(NULL);
    if(env == NULL) {
        printf("MLInitialize: internal error\n");
        return;
    }
    link = MLOpenArgcArgv(env, argc, argv, &error);
    if(link == NULL || error != MLEOK) {
        printf("MLOpen: internal error: %s\n", MLErrorMessage(link));
        return;
    }

    /* check validity of connection */
    if(!MLActivate(link)) {
        printf("MathLink connection failed\n");
        printf("MLActivate: %s\n", MLErrorMessage(link));
        return;
    }
}
